import 'dart:convert';

import 'package:largefriends/entity/json/json_parse_utils.dart';

import 'lf_location_entity.dart';

class LfMatchFilterEntity {
  LfMatchFilterEntity({
    this.minAge = 40,
    this.maxAge = 99,
    this.gender = 0,
    this.religion = 0,
    this.ethnicity = 0,
    this.drink = 0,
    this.haveChildren = 0,
    this.smoke = 0,
    this.locationEntity,
  });

  Map<String, dynamic> toJson() => <String, dynamic>{
        'minAge': minAge,
        'maxAge': maxAge,
        'gender': gender,
        'distance': distance,
        'ethnicity': ethnicity,
        'religion': religion,
        'smoke': smoke,
        'haveChildren': haveChildren,
        'drink': drink,
        'searchOrder': searchOrder,
        'locationEntity': locationEntity,
      };

  // int? seeking = TypeConfig.instance.getEntityListTotalKeys(
  //     TypeConfig.instance.getEntityLitByType(ConfigType.matchGender));

  int minAge = 40;
  int maxAge = 99;
  int drink = 0;
  int haveChildren = 0;
  int? distance = 0;
  String? searchOrder = '5';
  LfLocationEntity? locationEntity;

  ///用来记录livein的信息
  int gender = 0;
  int religion = 0;
  int ethnicity = 0;
  int smoke = 0;

  Map<String, dynamic> getSearchMap() {
    Map<String, dynamic> requestMap = {};
    if (minAge >= maxAge) {
      minAge = 40;
      maxAge = 99;
    }
    requestMap.putIfAbsent("find[minAge]", () => minAge < 40 ? 40 : minAge);
    requestMap.putIfAbsent("find[maxAge]", () => maxAge > 99 ? 99 : maxAge);
    requestMap.putIfAbsent("find[seeking]", () => gender > 0 ? gender : 3);
    // if (drink > 0) {
    //   requestMap.putIfAbsent("find[drink]", () => drink);
    // }
    // if (haveChildren > 0) {
    //   requestMap.putIfAbsent("find[children]", () => haveChildren);
    // }
    // requestMap.putIfAbsent("order", () => 'activity');
    //
    // if (religion > 0) {
    //   requestMap.putIfAbsent("find[religion]", () => religion);
    // }
    // if (ethnicity > 0) {
    //   requestMap.putIfAbsent("find[ethnicity]", () => ethnicity);
    // }
    //
    // if (smoke > 0) {
    //   requestMap.putIfAbsent("find[smoke]", () => smoke);
    // }

    if (locationEntity != null &&
        locationEntity!.countryId != null &&
        locationEntity!.countryId!.isNotEmpty) {
      requestMap.putIfAbsent("find[country]", () => locationEntity!.countryId);
    }
    if (locationEntity != null &&
        locationEntity!.stateId != null &&
        locationEntity!.stateId!.isNotEmpty) {
      requestMap.putIfAbsent("find[state]", () => locationEntity!.stateId);
    }
    // if (locationEntity != null &&
    //     locationEntity!.cityId != null &&
    //     locationEntity!.cityId!.isNotEmpty) {
    //   requestMap.putIfAbsent("find[city]", () => locationEntity!.cityId);
    // }
    return requestMap;
  }

  @override
  String toString() {
    return jsonEncode(this);
  }

  static LfMatchFilterEntity fromJson(Map<String, dynamic> map) {
    return LfMatchFilterEntity(
        minAge: map.asInt("minAge", 40),
        maxAge: map.asInt("maxAge", 99),
        gender: map.asInt("gender", 0),
        smoke: map.asInt("smoke", 0),
        drink: map.asInt("drink", 0),
        haveChildren: map.asInt("haveChildren", 0),
        religion: map.asInt("religion", 0),
        ethnicity: map.asInt("ethnicity", 0),
        locationEntity: map.asBean('locationEntity',
            (json) => LfLocationEntity.fromJson(json as Map<String, dynamic>)));
  }
}

const int anyWhere = 0;
const int liveIn = 1;
const int gpsLocation = 2;

const distanceMap = {
  1: '50 miles (80km)',
  2: '100 miles (160km)',
  3: '200 miles (320km)',
  4: '300 miles (480km)',
  5: '500 miles (800km)',
  6: '1000 miles (1600km)',
  7: 'Any distance'
};

const distanceValue = {
  "0": 0,
  "1": 50,
  "2": 100,
  "3": 200,
  "4": 300,
  "5": 500,
  "6": 1000,
  "7": 0
};
